#!/usr/bin/python

from ConfigParser import RawConfigParser as IniPatser

if __name__=='__main__':
    import os
    import sys
    import time

ConfigFile = '/backup/opensource-code/python_src/start-process/run-process.ini'
Section = 'program'
option = ('path', 'argvs')
def get_exec_command():
    if False==os.path.exists( ConfigFile ):
        print 'The config file run-process.ini is not exists, exit......'
        return None

    cfg = IniPatser()
    cfg.readfp( open(ConfigFile) )
    command = ''
    if True==cfg.has_section( Section ):
        for op in option:
            if True==cfg.has_option( Section, op ):
                command = command + cfg.get( Section, op ) + ' '
            else:
                print 'In the config file, the optin %s is not exists, exit......' %op
                return None
    else:
        print 'In the config file, the section %s is not exists, exit......' %Section
        return None

    return command

def main():
    command = get_exec_command()
    if None!=command:
        command = command + ' &'

    pid = -1
    try:
        pid = os.fork()
    except:
        print >>sys.stderr, 'fatal: os fork failed'
        print 'exit .....'
        sys.exit( 0 )

    if 0!=pid:
        print 'In the parent process, nothing to do, just exit ......'
        sys.exit( 0 )
    elif 0==pid:
        time.sleep( 2 )
        print 'In the child process, start emulator ......'
        try:
            #os.execl( execfile, argvs )
            print 'exec command : ', command
            if 0==os.system( command ):
                print 'start emulator successed ......!'
                sys.exit( 0 )
        except OSError, e:
            print 'error info : ', e.strerror
            print >>sys.stderr, 'faltal: exec the emulator failed ......'
            sys.exit( 0 )




if __name__=='__main__':
    main()

